package com.qs.eggs.account;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import com.qs.eggs.Constants;
import com.qs.eggs.R;


import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.Response;

public class ForgetPasswordActivity extends Activity implements View.OnClickListener{
    private EditText number,yanzhengma;
    private Button fasong,next;
    public static Activity instance;
    private String codeformService="";
    private CountDownTimer mTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        instance=this;
        initView();
    }
    private void initView(){
        number=(EditText)findViewById(R.id.et_number);
        yanzhengma=(EditText)findViewById(R.id.et_yanzhengma);
        fasong=(Button)findViewById(R.id.fasong);
        next=(Button)findViewById(R.id.next);

        fasong.setOnClickListener(this);
        next.setOnClickListener(this);
    }
    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.fasong:
                huoqu();
                break;
            case R.id.next:
                if(number.length()==0||yanzhengma.length()==0){
                    Toast.makeText(this,"手机号或验证码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!codeformService.equals("\""+yanzhengma.getText().toString()+"\"")){
                    Toast.makeText(this,"验证码填写不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent=new Intent(this,ForgetPasswordnextActivity.class);
                intent.putExtra("phone",number.getText().toString());
                startActivity(intent);
                break;
        }
    }
    private void huoqu(){
        final String txt = number.getText().toString();
        if (txt.equals("")) {
            Toast.makeText(this, "请输入手机号！", Toast.LENGTH_SHORT).show();
            return;
        }if(!isMobileNO(txt)){
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
        }else{
            checkPhone();

        }

    }
    private void checkPhone(){
        HashMap<String, String> map = new HashMap<>();
        map.put("phone",number.getText().toString() );
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"register?action=isExitsPhone")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (response.equals("\"true\"")){
                            Toast.makeText(ForgetPasswordActivity.this, "此手机号还未注册", Toast.LENGTH_LONG).show();
                        }else{
                            if (fasong.getTag() == null) {
                                //    mRequestType = 1;
                                fasong.setTag(true);
                                mTimer = new CountDownTimer(60 * 1000, 1000) {

                                    @SuppressLint("DefaultLocale")
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        fasong.setText(String.format("%s%s%d%s",
                                                "等待", "(", millisUntilFinished / 1000, ")"));
                                    }

                                    @Override
                                    public void onFinish() {
                                        fasong.setTag(null);
                                        fasong.setText("发送验证码");
                                    }
                                }.start();
                                HashMap<String, String> map = new HashMap<>();
                                map.put("phone", number.getText().toString());
                                Gson gson = new Gson();
                                String st = gson.toJson(map);

                                OkGo.post(Constants.URL_BASE + "register?action=getSMSCode")
                                        .params("params", st)
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onSuccess(String s, Call call, Response response) {
                                                codeformService = s;
                                            }
                                        });
                            }

                        }
                    }
                });


    }
    private boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("[1][34578]\\d{9}" );

        Matcher m = p.matcher(mobiles);

        return m.matches();

    }
}
